﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class Object
    {
        #region Properties
        #endregion

        #region Methods
        #endregion

        #region Static Methods
        public static List<Object> GetList()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Objects.OrderByDescending(p => p.ID);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }

        public static List<Object> GetList(int ID_Lang, int[] Ob)
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Objects.Where(p => p.ID_Language == ID_Lang && Ob.Contains(p.ID)).OrderByDescending(p => p.ID);
            if (v.Count() == 0)
                return null;
            return v.ToList();
        }


        private static List<ErrorItem> InputInfomationValidation(Object Object)
        {
            List<ErrorItem> errorList = new List<ErrorItem>();
            if (Object.ID_Language < 1)
                errorList.Add(new ErrorItem("ID_Language", "ID_Language is required"));
            if (Object.Description.Length > 30)
                errorList.Add(new ErrorItem("Description", "Description is only allow up to 30 characters."));
            return errorList;
        }

        public static List<ErrorItem> Insert(ref Object Object)
        {
            List<ErrorItem> errorList = InputInfomationValidation(Object);
            if (errorList.Count == 0)
            {
                System.Object ob = (System.Object)Object;
                if (!Provider.Insert(ref ob, "Objects"))
                    errorList.Add(new ErrorItem("Insert", "Failed."));
            }
            return errorList;
        }
        #endregion
    }
}